#include "fileStack.h"

int initFileStack(fileStack_t* file_stack) {
    file_stack->current_statck_top = 0;
    return 0;
}
int pushFileStack(fileStack_t* file_stack,const char* path) {
    if(file_stack->current_statck_top >= MAX_STACK_DEPTH) {
        // 栈溢出
        return -1;
    }
    // printf("压栈:%s\n",path);
    int size = strlen(path);
    file_stack->path[file_stack->current_statck_top] = (char*)calloc(size + 1,sizeof(char));
    memcpy(file_stack->path[file_stack->current_statck_top],path,size);
    ++file_stack->current_statck_top;
    return 0;
}
int popFileStack(fileStack_t* file_stack) {
    if(file_stack->current_statck_top <= 0) {
        // 空栈
        return -1;
    }
    --file_stack->current_statck_top;
    // printf("出栈:%s\n",file_stack->path[file_stack->current_statck_top]);
    if(file_stack->path[file_stack->current_statck_top] != NULL) {
        free(file_stack->path[file_stack->current_statck_top]);
        file_stack->path[file_stack->current_statck_top] = NULL;
    }
    return 0;
}
int isEmptyStack(fileStack_t* file_stack) {
    if(file_stack->current_statck_top == 0) {
        return 0;
    }
    return -1;
}

int printStack(fileStack_t file_stack) {
    printf("虚拟文件栈:\n");
    for(int i = 0;i < file_stack.current_statck_top;++i) {
        printf("%d : %s\n",i,file_stack.path[i]);
    }
    return 0;
} 
